*** This code predicts tax liability using TAXSIM
*
* Step 2. filing status :: T
*         dependents    :: T-1

cap log close
set more off
set type double
set linesize 150

adopath + "/home/projects/ota/ado/"
log using "${logdir}/get_data/post_sas/cdw_taxsim_2_yearT_filstat.log" , replace

*NOTE: TAXSIM will appropriately censor net STCG+LTCG at -3000


***********************************
* Load data and prepare variables *
***********************************

use cdw* prim_tin soi*yob nonfiler soi_fil_stat ///
	using "${statadir}/pre_taxsim_data.dta" , clear

gen byte TEMP_bad_fil_stat = !inrange(soi_fil_stat,1,4)

sort prim_tin TEMP_bad_fil_stat , stable
by   prim_tin: drop if _n>1

replace soi_fil_stat = 1 if TEMP_bad_fil_stat==1

gen year = 2019

gen state = 0
*NOTE: state=0 means TAXSIM will not calculate state rates

gen     mstat = 1 if soi_fil_stat==1 | soi_fil_stat==4
replace mstat = 2 if soi_fil_stat==2
replace mstat = 6 if soi_fil_stat==3
*mstat :: filing status: 1=single/hoh, 2=mfj, 6=mfs, 8=dependent taxpayer

gen     page = year - soi_prim_yob
replace page = 36 if page>114
*page :: primary age
*TAXSIM returns an error if age exceeds 114

gen     sage = year - soi_sec_yob if mstat==2
replace sage = 36                 if mstat==2 & sage>114
replace sage = 0                  if mstat!=2
*sage :: secondary age
*TAXSIM returns an error if age exceeds 114

mvencode cdw_dep* , mv(0) override

gen depx = cdw_depx
*depx :: number of dependents

gen dep6 = cdw_deps13
*dep6 :: ???

gen dep13 = cdw_deps13
*dep13 :: number of children under 13 with eligible child care expenses (Dependent Care Credit)

gen dep17 = cdw_deps17
*dep17 :: Number of children under 17 for the entire tax year (Child Credit)

gen dep18 = cdw_deps18
*dep18 :: number of EITC-qualifying children (typically younger than 19 or younger than 24 and a full-time student)

gen dep19 = cdw_deps18
*dep19 :: ???

gen pwages = cdw_wages_prim
*pwages :: primary wages

gen     swages = cdw_wages_sec if mstat==2
replace swages = 0             if mstat!=2
*swages :: secondary wages

gen dividends = max(0,cdw_qdiv)
*dividends :: qualified dividends

gen intrec = cdw_intrec
*intrec :: interest received

gen stcg = cdw_stcg - cdw_st_loss_cf
*stcg :: short-term capital gains or losses

gen ltcg = cdw_ltcg - cdw_lt_loss_cf
*ltcg :: long-term capital gains or losses

gen otherprop = cdw_otherprop
*otherprop :: other income subject to NIIT, including...
*             passive pass-through income,
*             rent not eligible for QBI deduction,
*             non-qualified dividends, etc.

gen nonprop = cdw_nonprop
*nonprop :: other income NOT subject to NIIT, including...
*           alimony,
*           nonwage fellowships,
*           state income tax refunds (itemizers only), etc.
*
*           the following can be included in NONPROP as negative income:
*           alimony paid,
*           Keogh and IRA contributions,
*           foreign income exclusion,
*           NOLs, etc.

gen pensions = cdw_pensions
*pensions :: taxable pensions and IRA distributions

gen gssi = cdw_gssi
*gssi :: gross social security benefits

gen  ui = cdw_ui
gen pui = cdw_ui
*ui/pui :: unemployment compensation received (assumed to be for the primary filer)

gen transfers = 0
*transfers :: other non-taxable transfer income, including...
*             welfare,
*             worker's comp,
*             veterans benefits,
*             child support that would affect eligibility for state property tax rebates but would not be taxable at the federal level, etc.

gen rentpaid = 0
*rentpaid :: rent paid (used only for calculating state property tax rebates)

gen proptax = 0
*proptax :: real estate taxes paid. this is an AMT preference and is also used to calculate state property tax rebates

gen otheritem = 0
*otheritem :: other itemized deductions that are AMT preferences, including...
*             other state and local taxes (line 8 of Sched A) plus local income tax,
*             preference share of medical expenses,
*             miscellaneous (line 27 of Sched A)

gen childcare = 0
*childcare :: childcare expenses

gen mortgage = 0
*mortgage :: deductions not included in OTHERITEM and not a preference for the AMT, including (on Sched A for 2009)...
*            deductible medical expenses not included in Line 16
*            motor vehicle taxes paid (line 7)
*            home mortgage interest (line 15)
*            charitable contributions (line 19)
*            casulty or theft losses (line 20)

gen scorp = 0
*scorp :: active s-corp SSTB income

gen TEMP_pbusinc = cdw_businc_prim
*pbusinc :: primary's non-SSTB QBI (w/ sufficient capital/wages to not phase out)
*           subject to SECA and Additional Medicare Tax

gen     TEMP_sbusinc = cdw_businc_sec if mstat==2
replace TEMP_sbusinc = 0              if mstat!=2
*sbusinc :: secondary's non-SSTB QBI (w/ sufficient capital/wages to not phase out)
*           subject to SECA and Additional Medicare Tax

gen pbusinc = TEMP_pbusinc
gen sbusinc = TEMP_sbusinc

replace pbusinc = 0                           if TEMP_pbusinc<0
replace sbusinc = TEMP_pbusinc + TEMP_sbusinc if TEMP_pbusinc<0

replace pbusinc = TEMP_pbusinc + TEMP_sbusinc if TEMP_sbusinc<0
replace sbusinc = 0                           if TEMP_sbusinc<0

replace pbusinc = 0 if (TEMP_pbusinc + TEMP_sbusinc)<0
replace sbusinc = 0 if (TEMP_pbusinc + TEMP_sbusinc)<0
*NOTE: TAXSIM fails to run when either individual has negative non-SSTB QBI
*      in such cases, if the sum of primary + secondary non-SSTB QBI is negative, we replace both with zeros
*      if the sum is nonnegative, we assign that sum to the one w/ positive non-SSTB QBI

gen pprofinc = 0
*pprofinc :: primary's SSTB QBI
*            subject to SECA and Additional Medicare Tax

gen sprofinc = 0
*sprofinc :: secondary's SSTB QBI
*            subject to SECA and Additional Medicare Tax

gen mtr = 0
*no idea why we need this, but it can't be missing values

drop TEMP* soi*yob

qui do ${postdir}/taxsim_student_interest.do



*******************************
* Run TAXSIM and save results *
*******************************

gen taxsimid = _n

preserve
	keep taxsimid mstat cdw_sav_cred_base depx dep17
	tempfile savers_credit_vars
	save `savers_credit_vars' , replace
restore

*NOTE: FICA calculations include the employer portion

cd $gdir
compress
taxsimlocal32 , replace full

merge 1:1 taxsimid using `savers_credit_vars' , nogen assert(match)

drop taxsimid

qui do ${postdir}/taxsim_savers_credit.do

local vv 2

rename fica   taxsim`vv'_fica
rename ficar  taxsim`vv'_mtr_fica
rename fiitax taxsim`vv'_fed_incm_tax
rename frate  taxsim`vv'_mtr_ordinc
rename v10    taxsim`vv'_agi
rename v18    taxsim`vv'_txbl_incm
rename v22    taxsim`vv'_ctc
rename v23    taxsim`vv'_addl_ctc
rename v24    taxsim`vv'_childcare_credit
rename v25    taxsim`vv'_eitc
rename v28    taxsim`vv'_incm_tax_b4_credits
rename v42    taxsim`vv'_qbi_ded

keep prim_tin nonfiler taxsim*

duplicates drop

compress
save "${statadir}/taxsim_output_2019_`vv'_filstat" , replace


cap log close


